<!--
 * @Author: your name
 * @Date: 2019-12-17 10:56:13
 * @LastEditTime: 2020-02-21 16:08:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-admin-template\src\views\account-manage\index.vue
 -->
<template>
  <div class="app-container">
    <el-card class="tableset" shadow="never" :body-style="{ padding: '0px' }">
      <!-- 小标题 -->
      <div>
        <span class="title">账号管理</span>
        <el-button
          type="primary"
          class="title-button-right"
          icon="el-icon-plus"
          plain
          @click="openAddAdminUserInfoDialog"
        >新增账号</el-button>
      </div>
      <!-- 小标题 -->

      <!-- 搜索关键词 -->
      <div class="funtion">
        <el-input
          v-model="get_admin_user_info_list.keyword"
          placeholder="请输入关键词"
          class="input-with-select"
          clearable
          @keyup.enter.native="getAdminUserInfoList"
        >
          <el-select
            slot="prepend"
            v-model="get_admin_user_info_list.keywordType"
            placeholder="请选择"
            class="option"
          >
            <el-option label="使用人名称" value="0" />
            <el-option label="账号" value="1" />
            <el-option label="联系电话" value="2" />
            <el-option label="联系邮箱" value="3" />
            <el-option label="备注" value="4" />
          </el-select>
          <el-button slot="append" icon="el-icon-search" @click="getAdminUserInfoList" />
        </el-input>
      </div>
      <!-- 搜索关键词 -->

      <!-- 数据表 -->
      <el-table
        v-loading="dataRequestLoading"
        element-loading-text="正在加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.5)"
        class="table"
        :data="data_admin_user_info_list.data"
        highlight-current-row
        stripe
        style="width: 100%;"
        :height="tableHeight"
        :max-height="tableHeight"
        @filter-change="changefilter"
        @selection-change="getSelection"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          label="所在区域"
          width="180"
          :filters="data_admin_user_info_list.filter_area_id"
          :filter-multiple="false"
          column-key="filterAreaId"
        >
          <template slot-scope="{ row }">
            <span>{{ area_id_text(row.area_id) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="账号" width="180" />
        <el-table-column
          label="权限角色"
          :filters="data_admin_user_info_list.filter_permission_role_id"
          :filter-multiple="false"
          column-key="filterPermissionRoleId"
        >
          <template slot-scope="{ row }">
            <span>{{ permission_role_id_text(row.permission_role_id) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="使用人名称" />
        <el-table-column prop="telephone" label="联系电话" />
        <el-table-column prop="email" label="联系邮箱" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="账号状态" width="180">
          <template slot-scope="{ row }">
            <el-tag :type="status[row.status].color" effect="dark">{{ status[row.status].label }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180px">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              @click="openEditAdminUserInfoDialog(row)"
            >编 辑</el-button>
            <el-button
              type="warning"
              @click="openEditChangePasswordDialog(row)"
            >修改密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 数据表 -->

      <!-- 数据表左下方功能区 -->
      <div>
        <el-button
          type="danger"
          class="card-bottom-button-left"
          icon="el-icon-delete"
          plain
          :disabled="deleteDisabled"
          @click="deleteAdminUserInfo"
        >删除账号</el-button>
      </div>
      <!-- 数据表左下方功能区 -->

      <!-- 分页 -->
      <el-pagination
        class="pagination"
        background
        layout="total, sizes,prev, pager, next, jumper"
        :total="data_admin_user_info_list.total"
        :page-size="get_admin_user_info_list.pageSize"
        :page-sizes="[20, 50, 100]"
        :current-page="get_admin_user_info_list.currentPage"
        @size-change="changePageSize"
        @current-change="changeCurrentPage"
      />
      <!-- 分页 -->
    </el-card>
    <!-- 编辑/新增账号信息dialog -->
    <el-dialog
      :title="change_admin_user_info_title"
      :visible.sync="changeAdminUserInfoDialog"
      width="600px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="changeAdminUserInfoRulesForm"
        :model="change_admin_user_info"
        status-icon
        size="mini"
        label-width="140px"
      >
        <el-form-item prop="avatar" :rules="rules.image" size="mini" label="上传头像 支持（ jpg/png/gif ）">
          <el-upload
            class="avatar-uploader"
            action
            :show-file-list="false"
            :auto-upload="false"
            :on-change="changeUploadImage"
          >
            <img v-if="change_admin_user_info.avatar" :src="showAvatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item prop="area_id" :rules="rules.select" size="mini" label="所在区域">
          <el-select v-model="change_admin_user_info.area_id" placeholder="请选择">
            <el-option
              v-for="item in data_admin_user_info_list.filter_area_id"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            />
          </el-select>
          <!-- <el-input v-model="change_admin_user_info.area_id" size="medium" placeholder="请输入所在区域" /> -->
        </el-form-item>
        <el-form-item prop="username" :rules="rules.input" size="mini" label="账号">
          <el-input v-model="change_admin_user_info.username" size="mini" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="permission_role_id" :rules="rules.select" size="mini" label="权限角色">
          <el-select v-model="change_admin_user_info.permission_role_id" placeholder="请选择权限角色">
            <el-option
              v-for="item in data_admin_user_info_list.filter_permission_role_id"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="name" :rules="rules.input" size="mini" label="使用人名称">
          <el-input v-model="change_admin_user_info.name" size="mini" placeholder="请输入使用人名称" />
        </el-form-item>
        <el-form-item prop="telephone" size="medium" label="联系电话">
          <el-input v-model="change_admin_user_info.telephone" size="mini" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item prop="email" size="medium" label="联系邮箱">
          <el-input v-model="change_admin_user_info.email" size="mini" placeholder="请输入联系邮箱" />
        </el-form-item>
        <el-form-item prop="remark" size="medium" label="备注">
          <el-input v-model="change_admin_user_info.remark" size="mini" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item prop="status" :rules="rules.select" size="mini" label="账号状态">
          <el-radio-group v-model="change_admin_user_info.status" size="mini">
            <el-radio-button label="1">启用</el-radio-button>
            <el-radio-button label="0">停用中</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="change_admin_user_info_title === '新增账号信息'"
          prop="password"
          size="mini"
          :rules="rules.input"
          label="设定密码"
        >
          <el-input
            v-model="change_admin_user_info.password"
            size="medium"
            placeholder="请输入备注"
            show-password
          />
        </el-form-item>
        <el-form-item
          v-if="change_admin_user_info_title === '编辑账号信息'"
          style="margin-top:5px"
          prop="create_time"
          size="mini"
          label="账号创建时间"
        >
          <span size="medium">{{ change_admin_user_info.create_time }}</span>
        </el-form-item>
        <el-form-item
          v-if="change_admin_user_info_title === '编辑账号信息'"
          prop="modified_time"
          size="mini"
          label="最后修改时间"
        >
          <span size="medium">{{ change_admin_user_info.modified_time }}</span>
        </el-form-item>
        <el-form-item
          v-if="change_admin_user_info_title === '编辑账号信息'"
          prop="login_time"
          size="mini"
          label="最后登陆时间"
        >
          <span size="medium">{{ change_admin_user_info.login_time }}</span>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="changeAdminUserInfoDialog = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="changeAdminUserInfo">保 存</el-button>
      </span>
    </el-dialog>
    <!-- 编辑账号信息dialog -->

    <!-- 修改密码dialog -->
    <el-dialog
      title="修改密码"
      :visible.sync="changePasswordDialog"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="changePassword"
        :model="change_password"
        status-icon
        size="medium"
        label-width="110px"
      >
        <el-form-item prop="status" size="medium" label="新密码">
          <el-input
            v-model="change_password.password"
            size="medium"
            show-password
            placeholder="请输入新密码"
          />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="changePasswordDialog = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="changePassword">保 存</el-button>
      </span>
    </el-dialog>
    <!-- 修改密码dialog -->

    <!-- 剪切图片dialog -->
    <el-dialog title="图片剪裁" :visible.sync="UploadAvatarPathDialog" :close-on-click-modal="false">
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
          <vueCropper
            ref="cropper"
            :img="option.img"
            :info="option.info"
            :output-size="option.size"
            :output-type="option.outputType"
            :can-scale="option.canScale"
            :auto-crop="option.autoCrop"
            :auto-crop-width="option.autoCropWidth"
            :auto-crop-height="option.autoCropHeight"
            :fixed-box="option.fixedBox"
            :fixed="option.fixed"
            :fixed-number="option.fixedNumber"
            :full="option.full"
            :can-move-box="option.canMoveBox"
            :original="option.original"
            :center-box="option.centerBox"
            :info-true="option.infoTrue"
          />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="UploadAvatarPathDialog = false">取 消</el-button>
        <el-button size="mini" type="primary" :loading="uploadImageLoading" @click="upLoadImage">确认</el-button>
      </div>
    </el-dialog>
    <!-- 剪切图片dialog -->
  </div>
</template>

<script>
import { getFormData } from '@/common/js/until'
import { uploadImage } from '@/api/upload'
import {
  getAdminUserInfoList,
  changeAdminUserInfo,
  changePassword,
  deleteAdminUserInfo
} from '@/api/account-manage'
export default {
  name: 'Index',
  data() {
    return {
      // table高度自适应
      tableHeight: window.innerHeight - 265,

      // loading开关
      dataRequestLoading: false,
      uploadImageLoading: false,
      // 展示头像
      showAvatar: '',
      // 图片裁剪框
      option: {
        img: '', // 裁剪图片的地址
        info: true, // 裁剪框的大小信息
        outputSize: 0.8, // 裁剪生成图片的质量
        outputType: 'png', // 裁剪生成图片的格式
        canScale: true, // 图片是否允许滚轮缩放
        autoCrop: true, // 是否默认生成截图框
        autoCropWidth: 700, // 默认生成截图框宽度
        autoCropHeight: 700, // 默认生成截图框高度
        fixedBox: true, // 固定截图框大小 不允许改变
        fixed: true, // 是否开启截图框宽高固定比例
        fixedNumber: [1, 1], // 截图框的宽高比例
        full: true, // 是否输出原图比例的截图
        canMoveBox: false, // 截图框能否拖动
        original: false, // 上传图片按照原始比例渲染
        centerBox: true, // 截图框是否被限制在图片里面
        infoTrue: true // true 为展示真实输出图片宽高 false 展示看到的截图框宽高
      },
      // 按钮开关
      deleteDisabled: true,
      // dialog开关
      changeAdminUserInfoDialog: false, // 编辑新增
      changePasswordDialog: false, // 修改密码
      UploadAvatarPathDialog: false, // 图片剪辑dialog
      // 账号状态栏显示
      status: {
        0: { color: 'danger', label: '停用中' },
        1: { color: 'success', label: '启用' }
      },
      // 发送请求参数
      get_admin_user_info_list: {
        pageSize: 20,
        currentPage: 1,
        keyword: '',
        keywordType: '0',
        sort_name: '',
        sort: 'desc',
        filterStatus: '',
        filterPermissionRoleId: '',
        filterAreaId: ''
      },
      // 发送修改密码请求参数
      change_password: {
        admin_user_id: '',
        password: ''
      },

      // 返回参数
      data_admin_user_info_list: {
        data: [],
        filter_permission_role_id: [],
        filter_area_id: [],
        total: 0
      },
      // 新增/编辑参数dialog标题
      change_admin_user_info_title: '',
      // 新增/编辑参数
      change_admin_user_info: {
        admin_user_id: '',
        name: '',
        area_id: '',
        username: '',
        status: '',
        permission_role_id: '',
        avatar: '',
        telephone: '',
        email: '',
        remark: '',
        password: '',
        create_time: '',
        modified_time: '',
        login_time: ''
      },
      // 表单校验规则
      rules: {
        input: {
          required: true,
          message: '请完成输入！',
          trigger: 'blur'
        },
        select: {
          required: true,
          message: '请完成选择！',
          trigger: 'blur'
        },
        image: {
          required: true,
          message: '请完成输入！',
          trigger: 'blur'
        }
      },
      value: ''
    }
  },
  mounted() {
    this.getAdminUserInfoList() // 查询权限角色信息列表
  },
  methods: {
    // 查询账号信息列表
    getAdminUserInfoList() {
      this.dataRequestLoading = true
      getAdminUserInfoList(this.get_admin_user_info_list).then(res => {
        this.dataRequestLoading = false
        this.data_admin_user_info_list.data = res.result.showing
        this.data_admin_user_info_list.filter_permission_role_id =
          res.result.filter_permission_role_id
        this.data_admin_user_info_list.filter_area_id =
          res.result.filter_area_id
        this.data_admin_user_info_list.total = res.result.total
      })
    },
    // 所在区域显示
    area_id_text(data) {
      const area_id = this.data_admin_user_info_list.filter_area_id.filter(
        v => {
          if (v.value === data) {
            return v.text
          }
        }
      )
      return area_id[0].text
    },
    // 权限角色显示
    permission_role_id_text(data) {
      const permission_role_id = this.data_admin_user_info_list.filter_permission_role_id.filter(
        v => {
          if (v.value === data) {
            return v.text
          }
        }
      )
      return permission_role_id[0].text
    },
    // 新增/修改账号信息
    changeAdminUserInfo() {
      this.$refs.changeAdminUserInfoRulesForm.validate(valid => {
        if (valid) {
          changeAdminUserInfo(this.change_admin_user_info).then(res => {
            this.getAdminUserInfoList()
            this.changeAdminUserInfoDialog = false
          })
        } else {
          return false
        }
      })
    },
    // 修改账号密码
    changePassword() {
      this.$refs.changePassword.validate(valid => {
        if (valid) {
          changePassword(this.change_password).then(res => {
            this.getAdminUserInfoList()
            this.changePasswordDialog = false
          })
        } else {
          return false
        }
      })
    },
    // 打开编辑账号信息dialog界面
    openEditAdminUserInfoDialog(row) {
      this.change_admin_user_info_title = '编辑账号信息'
      this.change_admin_user_info = {
        admin_user_id: row.admin_user_id,
        name: row.name,
        area_id: row.area_id,
        username: row.username,
        status: row.status,
        permission_role_id: row.permission_role_id,
        avatar: row.avatar,
        telephone: row.telephone,
        email: row.email,
        remark: row.remark,
        password: row.password,
        create_time: row.create_time,
        modified_time: row.modified_time,
        login_time: row.login_time
      }
      this.showAvatar = process.env.VUE_APP_BASE_API + row.avatar
      this.changeAdminUserInfoDialog = true
    },
    // 打开新增账号信息dialog界面
    openAddAdminUserInfoDialog() {
      this.change_admin_user_info_title = '新增账号信息'
      this.change_admin_user_info = {
        admin_user_id: '',
        name: '',
        area_id: '',
        username: '',
        status: '1',
        permission_role_id: '',
        avatar: '',
        telephone: '',
        email: '',
        remark: '',
        password: '',
        create_time: '',
        modified_time: '',
        login_time: ''
      }
      this.changeAdminUserInfoDialog = true
    },
    // 打开修改密码dialog界面
    openEditChangePasswordDialog(row) {
      this.change_password.admin_user_id = row.admin_user_id
      this.changePasswordDialog = true
    },
    // 删除账号信息（逻辑删除）
    deleteAdminUserInfo() {
      this.$confirm('此操作将永久删除该打印机档案信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const admin_user_id = []
          this.selection.forEach(val => {
            admin_user_id.push(val.admin_user_id)
          })
          deleteAdminUserInfo({ admin_user_id: admin_user_id }).then(res => {
            this.$notify({
              title: '成功',
              message: '删除打印机档案信息成功！',
              type: 'success'
            })
            this.getAdminUserInfoList()
          })
        })
        .catch(() => {
          this.$message({ type: 'info', message: '已取消删除' })
        })
    },

    // 获取选择项
    getSelection(selection) {
      if (selection.length === 0) {
        this.deleteDisabled = true
      } else {
        this.deleteDisabled = false
      }
      this.selection = selection
    },
    // 切换每页条数方法
    changePageSize(data) {
      this.get_admin_user_info_list.pageSize = data
      this.getAdminUserInfoList()
    },

    // 切换页码方法
    changeCurrentPage(data) {
      this.get_admin_user_info_list.currentPage = data
      this.getAdminUserInfoList()
    },
    // 打开剪切界面或直接上传gif
    changeUploadImage(file, fileList) {
      const isLt5M = file.size / 1024 / 1024 < 2
      if (!isLt5M) {
        this.$message.error('上传图片大小不能超过 2MB!')
        fileList.pop()
        return false
      }
      if (file.raw.type === 'image/gif') {
        const formData = getFormData(file.raw, 'avatarGif')
        uploadImage(formData).then(res => {
          this.change_admin_user_info.avatar = res.result
          this.showAvatar = process.env.VUE_APP_BASE_API + res.result
        })
      } else {
        if (file.raw.type !== 'image/jpeg' && file.raw.type !== 'image/png') {
          this.$message.error('上传图片只支持jpeg和png!')
          fileList.pop()
          return false
        }
        // 将图片地址赋值给裁剪框显示图片
        this.$nextTick(() => {
          fileList.pop()
          this.option.img = URL.createObjectURL(file.raw)
          this.UploadAvatarPathDialog = true
        })
      }
    },
    // 上传剪切后的图片
    upLoadImage() {
      this.$refs.cropper.getCropBlob(data => {
        const formData = getFormData(data, 'avatar')
        uploadImage(formData).then(res => {
          this.change_admin_user_info.avatar = res.result
          this.showAvatar = process.env.VUE_APP_BASE_API + res.result
          this.UploadAvatarPathDialog = false
        })
      })
    },

    // 筛选记录者
    changefilter(filters) {
      console.log(filters)
      if (filters.filterAreaId) {
        if (filters.filterAreaId.length === 0) {
          this.get_admin_user_info_list.filterAreaId = ''
        } else {
          this.get_admin_user_info_list.filterAreaId = filters.filterAreaId[0]
        }
      } else if (filters.filterPermissionRoleId) {
        if (filters.filterPermissionRoleId.length === 0) {
          this.get_admin_user_info_list.filterPermissionRoleId = ''
        } else {
          this.get_admin_user_info_list.filterPermissionRoleId =
            filters.filterPermissionRoleId[0]
        }
      }
      this.getAdminUserInfoList()
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader-user {
  padding-bottom: 1rem;
  position: relative;
  text-align: center;
}
</style>
